//
//  RestV1.m
//  MySpaceID
//
//  Created by Haider Sabri on Monday6/8/09.
//  Copyright 2009 MySpace, Inc. All rights reserved.
//

#import "RestV1.h"
#import "OAServiceTicket.h"
#import "V1Endpoints.h"

@implementation RestV1

@synthesize context;
@synthesize responseData;

NSString *methodCall = @"";


+ (RestV1*) apiWithContext:(SecurityContext*) context
{
	RestV1 *api = [[[RestV1 alloc] init]autorelease];
	api.context = [context retain];
	return api;
}

- (void) dealloc{

	[context release];
	self.responseData = nil;
	[super dealloc];
}

#pragma mark -
#pragma mark API Methods

- (void) clearData{
	// reserved function for future clean up
}

- (NSData*) getCurrentUser
{
	[self clearData];
	if(context)
	{
		NSURL *url = (NSURL*)[NSURL URLWithString: V1_CURRENT_USER];
		[context makeRequest:url :@"GET":nil:self];
		
	}
	return responseData;
	
}

- (NSData*) getUser:(NSInteger) userId
{
	[self clearData];
	if(context)
	{
		NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:V1_USER_ID, [NSString stringWithFormat: @"%d", userId]]];
		[context makeRequest:url :@"GET":nil:self];
	}
	return responseData;
	
}

- (NSData*) getMood:(NSInteger) userId
{
	[self clearData];
	if(context)
	{
		NSString *sUserId = [NSString stringWithFormat: @"%d", userId];
		NSString *urlString = [NSString stringWithFormat:V1_MOOD, sUserId];
		
		NSURL *url =  [NSURL URLWithString: urlString];
		[context makeRequest:url :@"GET":nil:self];

	}
	return responseData;
}

- (NSData*) getMoodsList: (NSInteger) userId
{
	[self clearData];
	if(context)
	{
		//NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:V1_MOODS, [NSString stringWithFormat: @"%d", userId]]];
		NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:V1_MOODS, [NSString stringWithFormat: @"%d", userId]]];
		[context makeRequest:url :@"GET":nil:self];
		[url release];
	}
	return responseData;
	
}
- (void) setMoodStatus:(NSInteger)userId moodId:(NSInteger)moodId moodName:(NSString*)moodName moodPictureName:(NSString*)moodPictureName status:(NSString*) status
{
	[self clearData];
	if(context)
	{
		NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:V1_STATUS, [NSString stringWithFormat: @"%d", userId]]];
		NSString *body = @"";
		if(moodId && moodId > 0)
			body = [body stringByAppendingFormat:@"moodid=%@&", moodId];
		if(moodName && [moodName length] > 0)
			body = [body stringByAppendingFormat:@"moodName=%@&", moodName];
		if(moodPictureName && [moodPictureName length] >0)
			body = [body stringByAppendingFormat:@"moodPictureName=%@&", moodPictureName];
		if(status)
			body = [body stringByAppendingFormat:@"status=%@&", status];	
		
		body = [body substringToIndex:[body length] - 1];

		[context makeRequest:url :@"PUT":body:self];
	}
}

- (NSData*) getAlbums:(NSInteger)userId
{
	return [self getAlbums:userId page:(NSInteger)nil pageSize: (NSInteger)nil];
}

- (NSData*) getAlbums:(NSInteger)userId page:(NSInteger)page pageSize:(NSInteger)pageSize
{
	[self clearData];
	if(context)
	{
		NSString *urlString = [NSString stringWithFormat:V1_ALBUMS, [NSString stringWithFormat: @"%d", userId]];
		if(page && pageSize)
		{
			urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"?page=%@&pagesize=%@",  [NSString stringWithFormat: @"%d", page],  [NSString stringWithFormat: @"%d", pageSize]]];
		}
		NSURL *url = [NSURL URLWithString: urlString];
		[context makeRequest:url :@"GET":nil:self];
	}
	return responseData;
	
}
- (NSData*) getAlbum: (NSInteger) userId albumId: (NSInteger) albumId
{
	[self clearData];
	if(context)
	{
		NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:V1_ALBUM, [NSString stringWithFormat: @"%d", userId],
					  [NSString stringWithFormat: @"%d", albumId]]];
		[context makeRequest:url :@"GET":nil:self];
	}
	return responseData;
	
}

- (NSData*) getFriends: (NSInteger) userId
{
	return [self getFriends:userId page:(NSInteger)nil pageSize:(NSInteger)nil];
}

- (NSData*) getFriends: (NSInteger) userId page:(NSInteger) page pageSize:(NSInteger) pageSize
{
	[self clearData];
	if(context)
	{
		NSString *urlString = [NSString stringWithFormat:V1_FRIENDS, [NSString stringWithFormat: @"%d", userId]];
		if(page && pageSize)
		{
			urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"?page=%@&pagesize=%@",  [NSString stringWithFormat: @"%d", page],  [NSString stringWithFormat: @"%d", pageSize]]];
		}
		NSURL *url = [[NSURL alloc] initWithString:urlString ];
		[context makeRequest:url :@"GET":nil:self];
		[url release];
	}
	return responseData;
	
}
//Need to add another call for getFriends to support FriendList and FriendShow parameters
- (NSData*) getFriendStatus: (NSInteger) userId
{
	[self clearData];
	if(context)
	{
		NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:V1_FRIEND_STATUS, [NSString stringWithFormat: @"%d", userId]]];
		[context makeRequest:url :@"GET":nil:self];
	}
	return responseData;
	
}

- (NSData*) getFriendship: (NSInteger) userId friendIds: (NSArray*) friendIds
{
	[self clearData];
	if(context)
	{
		NSString *friendsList = @"";
		for (NSString *friendId in friendIds) {
		         friendsList = [friendsList stringByAppendingString:[NSString stringWithFormat:@"%@;", friendId]];
		}
		friendsList = [friendsList substringToIndex:[friendsList length] - 1];
		NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:V1_FRIENDSHIP, [NSString stringWithFormat: @"%d", userId],
					  friendsList]];
		[context makeRequest:url :@"GET":nil:self];
	}
	return responseData;
}

- (NSData*) getPhotos: (NSInteger) userId
{
	return [self getPhotos:userId page:(NSInteger)nil pageSize:(NSInteger)nil];
}

- (NSData*) getPhotos: (NSInteger) userId page: (NSInteger) page pageSize: (NSInteger) pageSize
{
	[self clearData];
	if(context)
	{
		NSString *urlString = [NSString stringWithFormat:V1_PHOTOS, [NSString stringWithFormat: @"%d", userId]];
		if(page && pageSize)
		{
			urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"?page=%@&pagesize=%@",  [NSString stringWithFormat: @"%d", page],  [NSString stringWithFormat: @"%d", pageSize]]];
		}
		NSURL *url = [NSURL URLWithString: urlString];
		[context makeRequest:url :@"GET":nil:self];
	}
	return responseData;
	
}
- (NSData*) getPhoto: (NSInteger) userId photoId: (NSInteger) photoId
{
	[self clearData];
	if(context)
	{
		NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:V1_PHOTO, [NSString stringWithFormat: @"%d", userId],
											[NSString stringWithFormat: @"%d", photoId]]];
		[context makeRequest:url :@"GET":nil:self];
	}
	return responseData;
	
}
- (NSData*) getFullProfile: (NSInteger) userId
{
	[self clearData];
	if(context)
	{
		NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:V1_PROFILE, [NSString stringWithFormat: @"%d", userId],
											@"full"]];
		[context makeRequest:url :@"GET":nil:self];
	}
	return responseData;
	
}
- (NSData*) getBasicProfile: (NSInteger) userId
{
	[self clearData];
	if(context)
	{
		NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:V1_PROFILE, [NSString stringWithFormat: @"%d", userId],
											@"basic"]];
		[context makeRequest:url :@"GET":nil:self];
	}
	return responseData;
}
- (NSData*) getExtendedProfile: (NSInteger) userId
{
	[self clearData];
	if(context)
	{
		NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:V1_PROFILE, [NSString stringWithFormat: @"%d", userId],
											@"extended"]];
		[context makeRequest:url :@"GET":nil:self];
	}
	return responseData;
}
- (NSData*) getStatus: (NSInteger) userId
{
	[self clearData];
	if(context)
	{
		NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:V1_STATUS, [NSString stringWithFormat: @"%d", userId]]];
		[context makeRequest:url :@"GET":nil:self];
	}
	return responseData;
	
}
- (NSData*) getVideos: (NSInteger) userId
{
	return [self getVideos:userId page:(NSInteger)nil pageSize:(NSInteger)nil];
}
- (NSData*) getVideos: (NSInteger) userId page: (NSInteger) page pageSize: (NSInteger) pageSize
{
	[self clearData];
	if(context)
	{
		NSString *urlString = [NSString stringWithFormat:V1_VIDEOS, [NSString stringWithFormat: @"%d", userId]];
		if(page && pageSize)
		{
			urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"?page=%@&pagesize=%@",  [NSString stringWithFormat: @"%d", page],  [NSString stringWithFormat: @"%d", pageSize]]];
		}
		NSURL *url = [NSURL URLWithString: urlString];
		[context makeRequest:url :@"GET":nil:self];
	}
	return responseData;
	
}
- (NSData*) getVideo: (NSInteger) userId videoId: (NSInteger) videoId
{
	[self clearData];
	if(context)
	{
		NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:V1_VIDEO, [NSString stringWithFormat: @"%d", userId],
											[NSString stringWithFormat: @"%d", videoId]]];
		[context makeRequest:url :@"GET":nil:self];
	}
	return responseData;
}


- (NSData*) getActivities: (NSInteger) userId
{
	[self clearData];
	if(context)
	{
		NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:V1_ACTIVITIES, [NSString stringWithFormat: @"%d", userId]]];
		[context makeRequest:url :@"GET":nil:self];
	}
	return responseData;
}
- (NSData*) getFriendActivities: (NSInteger) userId
{
	[self clearData];
	if(context)
	{
		NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:V1_FRIEND_ACTIVITIES, [NSString stringWithFormat: @"%d", userId]]];
		[context makeRequest:url :@"GET":nil:self];
	}
	return responseData;
}


- (NSData*) getGlobalAppData
{
	return [self getGlobalAppData:nil];
}


- (NSData*) getGlobalAppData: (NSArray*) keys
{
	[self clearData];
	if(context)
	{
		NSURL *url = nil;
		if(keys)
		{
			NSString *keyList = @"";
			for (NSString *key in keys) {
				keyList = [keyList stringByAppendingString:[NSString stringWithFormat:@"%@;", key]];
			}
			keyList = [keyList substringToIndex:[keyList length] - 1];
			url = [NSURL URLWithString: [NSString stringWithFormat:V1_GLOBAL_APP_DATA_KEYS, keyList]];
		}
		else
		{
			url = [NSURL URLWithString: V1_GLOBAL_APP_DATA];
		}
		[context makeRequest:url :@"GET":nil:self];
	}
	return responseData;
}


- (void) addGlobalAppData: (NSDictionary*) globalAppDataPairs
{
	[self clearData];
	if(context)
	{
		NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:V1_GLOBAL_APP_DATA]];
		NSString *body = @"";
		for (id key in globalAppDataPairs) {
			
			NSLog(@"key: %@, value: %@", key, [globalAppDataPairs objectForKey:key]);
			body = [body stringByAppendingFormat:@"%@=%@&", key, [globalAppDataPairs objectForKey:key]];
			
		}
		
		body = [body substringToIndex:[body length] - 1];
		
		[context makeRequest:url :@"PUT":body:self];
	}
}


- (void) deleteGlobalAppData: (NSArray*) keys
{
	[self clearData];
	if(context)
	{
		
		NSString *appendUri = @"";
		for (id key in keys) {
			
			//NSLog(@"key: %@, value: %@", key, [globalAppDataPairs objectForKey:key]);
			appendUri = [appendUri stringByAppendingFormat:@"%@;", key];
			
		}
		appendUri = [appendUri substringToIndex:[appendUri length] - 1];
		NSString *urlString = [NSString stringWithFormat:V1_GLOBAL_APP_DATA_KEYS, appendUri];
		NSURL *url = [NSURL URLWithString: urlString];
		
		
		[context makeRequest:url :@"DELETE":nil:self];
	}
	
}
- (void) addUserAppData: (NSInteger) userId userAppDataPairs: (NSDictionary*) userAppDataPairs
{
	[self clearData];
	if(context)
	{
		NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:V1_USER_APP_DATA,  [NSString stringWithFormat: @"%d", userId]]];
		NSString *body = @"";
		for (id key in userAppDataPairs) {
			body = [body stringByAppendingFormat:@"%@=%@&", key, [userAppDataPairs objectForKey:key]];
		}
		
		body = [body substringToIndex:[body length] - 1];
		
		[context makeRequest:url :@"PUT":body:self];
	}
}
- (void) deleteUserAppData: (NSInteger) userId keys: (NSArray*) keys
{
	[self clearData];
	if(context)
	{		
		NSString *appendUri = @"";
		for (id key in keys) {
			appendUri = [appendUri stringByAppendingFormat:@"%@;", key];
		}
		appendUri = [appendUri substringToIndex:[appendUri length] - 1];
		NSString *urlString = [NSString stringWithFormat:V1_USER_APP_DATA_KEYS,  [NSString stringWithFormat: @"%d", userId],  appendUri];
		NSURL *url = [NSURL URLWithString: urlString];
		
		[context makeRequest:url :@"DELETE":nil:self];
	}
	
}

- (NSData*) getUserAppData: (NSInteger) userId
{
	return [self getUserAppData:userId keys:nil];
}

- (NSData*) getUserAppData: (NSInteger) userId keys: (NSArray*) keys
{
	[self clearData];
	if(context)
	{
		NSURL *url = nil;
		if(keys)
		{
			NSString *keyList = @"";
			for (NSString *key in keys) {
				keyList = [keyList stringByAppendingString:[NSString stringWithFormat:@"%@;", key]];
			}
			keyList = [keyList substringToIndex:[keyList length] - 1];
			url = [NSURL URLWithString: [NSString stringWithFormat:V1_USER_APP_DATA_KEYS, [NSString stringWithFormat: @"%d", userId] ,keyList]];
		}
		else
		{
			url = [NSURL URLWithString: [NSString stringWithFormat:V1_USER_APP_DATA, [NSString stringWithFormat: @"%d", userId]]];
		}
		[context makeRequest:url :@"GET":nil:self];
	}
	return responseData;
	
}
- (NSData*) getFriendAppData: (NSInteger) userId
{
	return [self getFriendAppData:userId keys:nil];
}
- (NSData*) getFriendAppData: (NSInteger) userId keys: (NSArray*) keys
{
	[self clearData];
	if(context)
	{
		NSURL *url = nil;
		if(keys)
		{
			NSString *keyList = @"";
			for (NSString *key in keys) {
				keyList = [keyList stringByAppendingString:[NSString stringWithFormat:@"%@;", key]];
			}
			keyList = [keyList substringToIndex:[keyList length] - 1];
			url = [NSURL URLWithString: [NSString stringWithFormat:V1_USER_FRIENDS_APP_DATA_KEYS, [NSString stringWithFormat: @"%d", userId] ,keyList]];
		}
		else
		{
			url = [NSURL URLWithString: [NSString stringWithFormat:V1_USER_FRIENDS_APP_DATA, [NSString stringWithFormat: @"%d", userId]]];
		}
		[context makeRequest:url :@"GET":nil:self];
	}
	return responseData;
	
}
- (NSData*) getIndicators: (NSInteger) userId
{
	[self clearData];
	if(context)
	{
		NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:V1_INDICATORS, [NSString stringWithFormat: @"%d", userId]]];
		[context makeRequest:url :@"GET":nil:self];
	}
	return responseData;
	
}

- (void) sendNotification: (NSInteger) appId recipients: (NSArray*) recipients content: (NSString*) content button0Surface:(NSString*) button0Surface button0Label:(NSString*) button0Label button1Surface:(NSString*) button1Surface button1Label:(NSString*) button1Label mediaItem: (NSString*) mediaItem
{
	
}


#pragma mark -
#pragma mark OAResponseDelegate methods

- (void)apiTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data {
	NSString *dataOutput = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
	
	NSLog(dataOutput);
	NSLog( [[[ticket response] URL] absoluteString]);
	NSLog( [NSString stringWithFormat:@"%d", [(NSHTTPURLResponse*)[ticket response ] statusCode]]);
	if (ticket.succeeded) {
		self.responseData = data;
	}
	[dataOutput release];
}




-(void) apiTicket:(OAServiceTicket *)ticket didFinishWithError:(NSError *)error {
	
	NSLog(@"error came back");
}


@end



